package lww231;

import com.alibaba.fastjson.JSON;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;

public class JSONBufferedReader {
    public static void main(String args[]){
        try {
            BufferedReader br=new BufferedReader(new FileReader("Film.json"));
            FileWriter sw = new FileWriter(new File("Film.csv"));
            //MovieInfo 类可以参考实验1
            MovieInfo movieInfo = null;
            //StringBuffer stringBuffer = new StringBuffer();
            String line;
            while ((line = br.readLine()) != null) {
                //Fastjson 把每行的json 字符串转换为对象。
                movieInfo = JSON.parseObject(line, MovieInfo.class);
                //stringBuffer.append(line);
                if (movieInfo.getActor().indexOf("徐少强") != -1) {
                    //Film_page 作为电影ID
                    String id = movieInfo.getFilm_page();
                    //取出演员的列表
                    String[] actors = movieInfo.getActor().split(",");
                    for (String actor : actors) {
                        //把电影数据写入csv文件。csv 表头为 ID,电影名称,评分,演员
                        sw.append(id + "," + movieInfo.getTitle() + "," + actor + "," + movieInfo.getStar() + "\n");
                    }
                }
            }
            //关闭流
            sw.close();
            br.close();
            //System.out.println(stringBuffer);
        }catch (Exception e){
            e.printStackTrace();// 只能输出在控制台当中，日志文件看不到
            //System.out.println("Error in closing the BufferedReader");
        }
    }
}
